﻿using System.Linq.Expressions;
using BaseData.Infrastructrue.Interfaces;
using Microsoft.EntityFrameworkCore;

namespace BaseData.Infrastructrue.Inplements
{
    public class Basepository<T> : IBasepository<T> where T : class
    {
        private readonly Mycontext context;

        public Basepository(Mycontext _context)
        {
            context = _context;
        }
        public IQueryable<T> GetAll()
        {
            try
            {
                return context.Set<T>().AsQueryable();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public async Task<T> GetModel(Expression<Func<T,bool>> expression)
        {
            try
            {
                return await context.Set<T>().FirstOrDefaultAsync(expression);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public async Task<int> InsertAsync(T entity)
        {
            try
            {
                await context.Set<T>().AddAsync(entity);
                return await context.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public async Task<int> EditAsync(T entity)
        {
            try
            {
                context.Set<T>().Update(entity);
                return await context.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public async Task<int> DeleteAsync(T entity)
        {
            try
            {
                context.Set<T>().Remove(entity);
                return await context.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
